#include "cmp.h"
#include "gpio.h"
#include "rcc.h"
#include "system_remap.h"


/**
  * @brief  Config the specified CMP peripheral INPRefVoltage select.
  * @param  CMPx: where x can be 0,1 to select the CMPx INP peripheral INPRefVoltage select.
  * @param  RefVoltage: new value of the CMPx peripheral INPRefVoltage select.
		@param  RefVoltage This parameter can be one of the following values:
						CMP_INP0,CMP_INP1
  * @retval None
  */
void CMP_INPRefVoltageSet(CMP_TypeDef* CMPx,uint8_t CMP_INPx)
{	
        uint16_t cmpcr_reg = CMPx->CR;
	
		cmpcr_reg &= ~CMP_SELP_Set;
		cmpcr_reg |= (CMP_INPx<<2)&CMP_SELP_Set;		
	
		CMPx->CR = cmpcr_reg;
}

/**
  * @brief  Config the specified CMP peripheral INNRefVoltageSet.
  * @param  RefVoltage: new value of the CMPx peripheral INNRefVoltageSet.
		@param  RefVoltage This parameter can be one of the following values:
						CMP_TRIM_INN_0 ~~ CMP_TRIM_INN_63
  * @retval None
  */
void CMP_TRIMRefVoltageSet(CMP_TypeDef* CMPx,uint16_t RefVoltage)
{
		uint16_t cmpcr_reg = CMPx->CR;
	
		cmpcr_reg &= ~CMP_TRIM_Mask;
		cmpcr_reg |= (RefVoltage<<3)&CMP_TRIM_Mask;		
	
		CMPx->CR = cmpcr_reg;	
	
}


/**
  * @brief  CMP peripheral INT edge set.
  * @param  CMPx: where x can be 0,1, to select the CMP peripheral.
  * @param  CMP_INTx: new state of the CMPx peripheral INT Dir set.
  *   This parameter can be: CMP_INT_Up or CMP_INT_Down.
  * @retval None
  */
void CMP_INTDirSet(CMP_TypeDef* CMPx,uint16_t CMP_INTx)
{	
		uint16_t cmpcr_reg = CMPx->CR;
	
		if(CMP_INTx != CMP_INT_Down)
		{
			cmpcr_reg &= ~CMP_HY_Set;
			cmpcr_reg |= CMP_HY_Set;					
		}
		else
		{
			cmpcr_reg &= ~CMP_HY_Set;
		}
	
		CMPx->CR = cmpcr_reg;	
}
/**
  * @brief  Initializes the CMPx peripheral according to the specified parameters
  *         in the CMP_InitStructure.
  * @param  ADCx: where x can be 0,1 to select the CMP peripheral.
  * @param  CMP_InitStructure: pointer to an CMP_InitTypeDef structure that contains
  *         the configuration information for the specified CMP peripheral.
  * @retval None
  */

void CMP_Init(CMP_TypeDef* CMPx, CMP_InitTypeDef* CMP_InitStructure)
{	
	uint16_t cmpcr_reg = CMPx->CR;
	
	cmpcr_reg &= ~CMP_EN_Set;
	cmpcr_reg &= ~CMP_HY_Set;
	cmpcr_reg &= ~CMP_SELP_Set;
	cmpcr_reg &= ~CMP_TRIM_Mask;
	
	cmpcr_reg |= (CMP_InitStructure->INT_DIR<<1)&CMP_HY_Set;	
	cmpcr_reg |= (CMP_InitStructure->INP_SEL<<2)&CMP_SELP_Set;
	cmpcr_reg |= (CMP_InitStructure->TRIM<<3)&CMP_TRIM_Mask;	
	
	CMPx->CR = cmpcr_reg;
}

/**
  * @brief  Enables or disables the specified CMP peripheral.
  * @param  CMPx: where x can be 0,1,2,3  to select the CMP peripheral.
  * @param  NewState: new state of the CMPx peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CMP_Cmd(CMP_TypeDef* CMPx,FunctionalState NewState)
{
		uint16_t cmpcr_reg = CMPx->CR;

	
		if(NewState != DISABLE)
		{
			/*CMPEN CMP0 EN*/
			cmpcr_reg &= ~CMP_EN_Set;
			cmpcr_reg |= CMP_EN_Set;				
		}
		else
		{
			cmpcr_reg &= ~CMP_EN_Set;				
		}


		CMPx->CR = cmpcr_reg;
}


/**
  * @brief  Get CMPx peripheral output status
  * @param  CMPx: where x can be 0,1,2,3 to select the CMP peripheral.
  * @param  NewState: new state of the CMPx peripheral.
  *   This parameter can be: SET or RESET.
  * @retval None
  */
FlagStatus CMP_GetOutputStatus(CMP_TypeDef* CMPx)
{
	uint16_t cmpcr_reg = CMPx->CR;
	FlagStatus bitstatus = RESET;
	
	if(cmpcr_reg&CMP_OUT_FLAG)
	{
		bitstatus = SET;
	}
	else 
	{
		bitstatus = RESET;
	}

	
	return bitstatus;
}

void CMP_ITConfig(CMP_TypeDef* CMPx,FunctionalState NewState)
{
		uint16_t cmpcr_reg = CMPx->CR;

	
		if(NewState != DISABLE)
		{
			/*CMPEN CMP0 EN*/
			cmpcr_reg &= ~CMP_IT_Set;
			cmpcr_reg |= CMP_IT_Set;				
		}
		else
		{
			cmpcr_reg &= ~CMP_IT_Set;				
		}


		CMPx->CR = cmpcr_reg;
}

FlagStatus CMP_GetITStatus(CMP_TypeDef* CMPx)
{
	uint16_t cmpcr_reg = CMPx->CR;
	FlagStatus bitstatus = RESET;
	
	if(cmpcr_reg&CMP_IT_FLAG)
	{
		bitstatus = SET;
	}
	else 
	{
		bitstatus = RESET;
	}

	
	return bitstatus;	
}

void CMP_ClearITPendingBit(CMP_TypeDef* CMPx)
{
	uint16_t cmpcr_reg = CMPx->CR;
	
	cmpcr_reg |= CMP_IT_FLAG;
	
	CMPx->CR = cmpcr_reg;
}
